package com.jqsoft.launcher.activity.task;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jqsoft.launcher.activity.JQActivity;
import com.jqsoft.launcher.activity.bean.OlderPersonInfo;
import com.jqsoft.launcher.activity.bean.UserLoginInfo;
import com.jqsoft.launcher.common.BphsConstants;
import com.jqsoft.launcher.R;
import com.jqsoft.launcher.util.Log;

public class WebViewZYYActivity extends JQActivity {

    public WebView webView;
    private OlderPersonInfo olderPersonInfo;
    private String UserCode, cardNo, webIP, webName;
    private String url;

    @Override
    protected int getContentLayout() {
        return R.layout.webview_layout;
    }

    @Override
    protected void initGui() {
        webIP = BphsConstants.HTTP_PROTOCOL + BphsConstants.HTTP_SERVER_IP + ":" + BphsConstants.HTTP_SERVER_PORT;
        webName = "/RhipInterface.aspx?";
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            olderPersonInfo = (OlderPersonInfo) bundle.get("OlderPersonInfo");
            cardNo = olderPersonInfo.getsCardNo();
          //  UserCode = olderPersonInfo.getsAddUser();
        }
        UserLoginInfo info = UserLoginInfo.getInstances();
        UserCode = info.getsLoginName();
        webView = (WebView) this.findViewById(R.id.webView);
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
        // webView.requestFocus();
//		webView.getSettings().setUseWideViewPort(true);
//		webView.getSettings().setLoadWithOverviewMode(true);
        url = (webIP + webName + "iType=" + "99" + "&UserCode=" + UserCode + "&IDNO=" + cardNo).trim();
        //http://192.168.44.138:9998/RhipInterface.aspx?iType=99&UserCode=110921004502&IDNO=340123194906265029

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                Log.i("webview", "onProgressChanged called");
                WebViewZYYActivity.this.setProgress(progress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, final String message,
                                     JsResult result) {
                Log.i("webview", "onJsAlert called");
                final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                builder.setTitle("")
                        .setMessage(message)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent intent = getIntent();
                                intent.putExtra("message", message);
                                setResult(RESULT_OK, intent);
                                if(!message.equals("您有必填项未填写，请确认填写后再保存！")){
                                    finish();
                                }

                            }
                        });
                // 禁止响应按back键的事件
                builder.setCancelable(false);
                AlertDialog dialog = builder.create();
                dialog.show();
                result.confirm();
                return true;
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                loadWaitPanel();
                super.onPageStarted(view, url, favicon);
            }

            public void onPageFinished(WebView view, String url) {
                dismissrogressBar();
                super.onPageFinished(view, url);
            }
            // 这个方法在用户试图点开页面上的某个链接时被调用
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url!=null) {
                    // 如果想继续加载目标页面则调用下面的语句
                     view.loadUrl(url);
                    // 如果不想那url就是目标网址，如果想获取目标网页的内容那你可以用HTTP的API把网页扒下来。
                }
                // 返回true表示停留在本WebView（不跳转到系统的浏览器）
                return true;
            }
        });
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }
                return false;
            }
        });
        webView.loadUrl(url);
    }

    @Override
    protected void initAction() {
        setGoBack();
    }

    @Override
    protected void initData() {
        setTitle("老年人中医药服务");
    }

}
